package com.ruix.app.widget;

import android.app.Activity;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

//import com.hiwifi.RuiApplication;
//import com.hiwifi.R;

import com.ruix.app.R;
import com.ruix.app.RuiApplication;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by hadexs on 16/5/5.
 */
public class DynamicLoadingView {
    private static DynamicLoadingView fragment;

    public static DynamicLoadingView newInstance() {
        if (fragment == null) {
            fragment = new DynamicLoadingView();
        }
        return fragment;
    }

    private View displayView;
    private Activity attachedActivity;
    private final int msg_hide = 1;
    private final int minTime = 1000;
    private static final int navHeight = RuiApplication.getAppContext().getResources().getDimensionPixelSize(R.dimen.height_nav);
    long startTime;


    public void showLoading(Activity activity,boolean below){
        attachedActivity = activity;
        startTime = System.currentTimeMillis();
//        handler.removeMessages(msg_hide);
        if (displayView != null) {
            getRootView().removeView(displayView);
        }
        displayView = generateDisplayView();
        if (displayView != null) {
            getRootView().addView(displayView);
            View view = null; //attachedActivity.findViewById(R.id.nav);
            final Boolean hasNav = view != null;
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) displayView.getLayoutParams();
            if (hasNav || below) {
                layoutParams.height = getAvaiableScreenHeight() - navHeight - getHeightOfStatusBar();
                layoutParams.setMargins(0, navHeight + getHeightOfStatusBar(), 0, 0);
            } else {
                //高度不能用MATCH_PARENT，因为弹出键盘后,键盘占据了高度
                layoutParams.height = getAvaiableScreenHeight() - getHeightOfStatusBar();
                layoutParams.setMargins(0, getHeightOfStatusBar(), 0, 0);
            }

            displayView.setLayoutParams(layoutParams);
        }
    }

    public void showLoading(Activity activity) {
        showLoading(activity,false);
    }

    private View generateDisplayView() {
        View resultView = null;
        if (attachedActivity != null) {
            resultView = LayoutInflater.from(attachedActivity).inflate(R.layout.layout_loading, null);
        }
        return resultView;
    }


    public void removeLoading() {
        long time = System.currentTimeMillis() - startTime;
//        handler.sendEmptyMessageDelayed(msg_hide, Math.max(minTime - time, 0));
        removeLoadingInner();
    }

    private synchronized void removeLoadingInner() {
        if (displayView != null && attachedActivity != null) {
//            ((WaveRipViewSurface) displayView.findViewById(R.id.loading)).stop();
            getRootView().removeView(displayView);
        }
        attachedActivity = null;
    }


    public ViewGroup getRootView() {
        return (ViewGroup) attachedActivity.getWindow().peekDecorView();
    }


    private static DisplayMetrics sDisplay;

    public static int getScreenWidth() {
        if (sDisplay == null) {
            sDisplay = RuiApplication.getAppContext().getResources().getDisplayMetrics();
        }
        return sDisplay.widthPixels;
    }

    public static int getScreenHeight() {
        if (sDisplay == null) {
            sDisplay = RuiApplication.getAppContext().getResources().getDisplayMetrics();
        }
        return sDisplay.heightPixels;
    }

    public static int getAvaiableScreenHeight() {
        int height = getScreenHeight();
        if (hasSmartBar()) {
            height -= 120;
        }
        return height;
    }

    private static int heightOfStatusBar = 0;

    public static int getHeightOfStatusBar() {
        if (heightOfStatusBar == 0) {
            Object obj = null;
            Field field = null;
            Class<?> c = null;
            int x = 0;
            try {
                c = Class.forName("com.android.internal.R$dimen");
                obj = c.newInstance();
                field = c.getField("status_bar_height");
                x = Integer.parseInt(field.get(obj).toString());
                heightOfStatusBar = RuiApplication.getAppContext().getResources().getDimensionPixelSize(x);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }


        return heightOfStatusBar;
    }

    public static boolean hasSmartBar() {
        // 用Build.DEVICE判断[对魅族系列做个性判断]
        if ("m1".equals(Build.DEVICE) || Build.DEVICE.equals("mx") || Build.DEVICE.equals("m9")) {
            return false;
        } else if (Build.DEVICE.equals("mx2")) {
            return true;
        }
        try {
            // 新型号可用反射调用Build.hasSmartBar()
            Class clazz = Class.forName("android.os.Build");
            Method method;
            try {
                method = clazz.getMethod("hasSmartBar");
            } catch (NoSuchMethodException e) {
                return false;
            }
            if (method != null) {
                return ((Boolean) method.invoke(null)).booleanValue();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
